package whf.framework.security.acegiimp;

import java.io.Serializable;

import org.acegisecurity.GrantedAuthority;

import whf.framework.security.entity.Role;

/**
 * 用户所受的权限,系统中相当于给与用户的角色
 * @author wanghaifeng
 *
 */
public class GrantedAuthorityImp implements GrantedAuthority,Serializable {
	private Role role;
	
	public GrantedAuthorityImp(Role role){
		this.role = role;
	}
	
	public void init(Role role){
		this.role = role;
	}
	
	/* (non-Javadoc)
	 * @see org.acegisecurity.GrantedAuthority#getAuthority()
	 */
	public String getAuthority() {
		return this.role.getName();
	}

    public boolean equals(Object obj) {
        if (obj instanceof String) {
            return obj.equals(this.role.getName());
        }
        if (obj instanceof GrantedAuthority) {
            GrantedAuthority attr = (GrantedAuthority) obj;
            return this.role.getName().equals(attr.getAuthority());
        }
        return false;
    }

    public int hashCode() {
        return this.role.getName().hashCode();
    }

    public String toString() {
        return this.role.getName();
    }
}
